package org.spring.springboot.service.impl;

import org.apache.ibatis.annotations.Param;
import org.spring.springboot.dao.GraphFileDao;
import org.spring.springboot.domain.GraphFile;
import org.spring.springboot.service.GraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class GraphFileServiceImpl implements GraphService {

    @Autowired
    private GraphFileDao graphFileDao;

    public List<GraphFile> selectAll(String userid) {
        List<GraphFile> list = graphFileDao.selectAll(userid);
        return list;
    }

    public GraphFile selectOne(String uuid) {
        return graphFileDao.selectOne(uuid);
    }

    public void save(GraphFile graphFile) {
        graphFileDao.save(graphFile);
    }

    public void updateData(String uuid, String data, String iconURL,String extInfo) {

        graphFileDao.updateData(uuid, data, iconURL,extInfo);
    }

    public void updateProfile(String uuid, String sname, String parent, String iconURL) {
        graphFileDao.updateProfile(uuid, sname, parent, iconURL);
    }

    public void delete(String uuid) {
        graphFileDao.delete(uuid);
    }

    public List<GraphFile> selectChild(String pid,String userid) {
        //先获取所有图形
        List<GraphFile> list = graphFileDao.selectAll(userid);

        //子子孙孙都要返回
        List<GraphFile> validList = new ArrayList<>();

        //找到当前图形对象
        GraphFile rootObj = null;
        for (GraphFile p : list) {
            String uuid = p.getUuid();
            int cmpRet = uuid.compareTo(pid);
            if (cmpRet == 0) {
                rootObj = p;
                break;
            }
        }

        //找其子子孙孙放validList队列
        if (rootObj != null) {
            validList.add(rootObj);
            for (GraphFile p : list) {
                if (p != rootObj) {
                    String projid = p.getProjectid();
                    if (projid.compareTo(pid) == 0) {
                        validList.add(p);
                    }
                }
            }
        }
        return validList;
    }
}
